import { _decorator, Component, instantiate, math, Node, Prefab, RigidBody2D, Vec2, Vec3 } from 'cc';
import { BaseComponent } from './BaseComponent';
import { EntityVo } from '../vo/EntityVo';
import { Bullet } from '../entity/Bullet';
import { CacheManager } from '../../manager/CacheManager';
import Mgr from '../../manager/Mgr';
import { UIModuleEnum } from '../../enum/UIDefine';
import { CooldownComponent } from './CooldownComponent';
import { AttackRangeIndicator } from './AttackRangeIndicator';

const { ccclass, property } = _decorator;

@ccclass('AttackComponent')
export class AttackComponent extends BaseComponent {
    @property
    attackRange: number = 1000;

    @property
    attackInterval: number = 2;

    @property
    targetTag: string = 'Player';

    @property(Prefab)
    bulletPrefab: Prefab = null;

    @property(Node)
    rangeIndicatorPrefab: Node = null;

    private _target: Node = null;
    private _attackTimer: number = 0;
    private _canAttack: boolean = true;
    private _cdComponent: CooldownComponent = null;
    private _rangeIndicator: AttackRangeIndicator = null;

    init() {
        // 查找目标
        this._target = CacheManager.game.player.node;
        Mgr.loader.LoadUIPrefab(UIModuleEnum.main,"Bullet",(prefab:Prefab)=>{
            this.bulletPrefab = prefab;
        });
        this._cdComponent = this.node.getComponent(CooldownComponent);

        // 创建范围指示器
        this.createRangeIndicator();
    }

    private createRangeIndicator() {
        // if (!this.rangeIndicatorPrefab) return;

        // const indicatorNode = instantiate(this.rangeIndicatorPrefab);
        // this.node.addChild(indicatorNode);
        this._rangeIndicator = this.node.getChildByName("AttackRange").getComponent(AttackRangeIndicator);
        if (this._rangeIndicator) {
            this._rangeIndicator.init(this.attackRange);
        }
    }

    setData(vo: EntityVo): void {
        super.setData(vo);
        this._canAttack = true;
        this._attackTimer = 0;
    }

    update(deltaTime: number) {
        if (!this._isRunning || !this._target || !this._target.active) return;

        const inRange = this.isTargetInRange();
        
        // 更新范围指示器状态
        if (this._rangeIndicator) {
            this._rangeIndicator.setWarning(inRange);
        }

        // 更新攻击冷却
        if (this._attackTimer > 0) {
            this._attackTimer -= deltaTime;
            if (this._attackTimer <= 0) {
                this._canAttack = true;
            }
        }

        // 检查是否在攻击范围内
        if (this._canAttack && inRange) {
            this.attack();
        }
    }

    private isTargetInRange(): boolean {
        const selfWorldPos = this.node.getWorldPosition();
        const targetWorldPos = this._target.getWorldPosition();
        
        const distance = Vec2.distance(
            new Vec2(selfWorldPos.x, selfWorldPos.y),
            new Vec2(targetWorldPos.x, targetWorldPos.y)
        );
        return distance <= this.attackRange;
    }

    private attack() {
        if(!this.bulletPrefab) return;
        // 创建子弹
        const bulletNode = instantiate(this.bulletPrefab);
        const bullet = bulletNode.getComponent(Bullet);
        
        // 计算朝向目标的方向
        const selfWorldPos = this.node.getWorldPosition();
        const targetWorldPos = this._target.getWorldPosition();
        const direction = new Vec2(
            targetWorldPos.x - selfWorldPos.x,
            targetWorldPos.y - selfWorldPos.y
        ).normalize();

        // 将子弹添加到场景中并发射
        this.node.parent.addChild(bulletNode);
        bullet.init();
        bullet.shoot(selfWorldPos, direction);

        // 设置攻击冷却
        this._canAttack = false;
        this._attackTimer = this.attackInterval;
        
        // 启动CD显示
        if (this._cdComponent) {
            this._cdComponent.startCooldown(this.attackInterval);
        }
    }

    stop(immediate?: boolean): void {
        super.stop(immediate);
        this._canAttack = true;
        this._attackTimer = 0;
        if (this._cdComponent) {
            this._cdComponent.stopCooldown();
        }
        if (this._rangeIndicator) {
            this._rangeIndicator.node.destroy();
            this._rangeIndicator = null;
        }
    }
} 